package com.lqkj.maptest.controller;

import com.lqkj.maptest.entity.User;
import com.lqkj.maptest.service.UserService;
import com.lqkj.maptest.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Linm
 * @title
 * @date 2021/8/4 21:00
 */
@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 查询所有信息并分页
     */
    @GetMapping("/findAll")
    public ResultVo findAll(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        try {
            List<User> userList = userService.findAll(pageNum,pageSize);
            log.info("put num:{}", userList.size());
            return ResultVo.success(userList,"SUCCESS",userList.size());
        }catch (Exception e){
            return ResultVo.fail(500,"未知异常");
        }
    }


    /**
     * 添加用户信息
     */
    @PostMapping("/addUser")
    public ResultVo addUser(User user) {
       try {
           Integer num = userService.addUser(user);
           log.info("新增记录数为：{}", num);
           return ResultVo.success(user,"SUCCESS",num);
       }catch (Exception e){
           return ResultVo.fail(500,"未知异常");
           }
    }


    /**
     * 批量添加用户信息
     */
    @PostMapping("/batchAddUser")
    public ResultVo batchAddUser(@RequestBody List<User> users) {
            Integer num = userService.batchAddUser(users);
            log.info("新增记录数为：{}", num);
            return ResultVo.success(users,"SUCCESS",num);

    }
    /**
     * 删除用户信息
     */
    @GetMapping("/deleteUser")
    public ResultVo deleteUser(User user){
        try {
            Integer num = userService.deleteUser(user);
            log.info("删除记录数为：{}", num);
            return ResultVo.success(user,"SUCCESS",num);
        }catch (Exception e){
                return ResultVo.fail(500,"未知异常");
        }

    }

    /**
     * 批量删除用户信息
     */
    @PostMapping("/batchDeleteUser")
    public ResultVo batchDeleteUser(Integer [] ids){
        try {
            Integer num = userService.batchDeleteUser(ids);
            log.info("删除记录数为：{}", num);
            return ResultVo.success(ids,"SUCCESS",num);
        }catch (Exception e){
            if (ids == null){
                return ResultVo.success("未选中删除行");
            }
            return ResultVo.fail(500,"未知异常");
        }
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updateUser")
    public ResultVo updateUser(User user){
        try {
            Integer num = userService.updateUser(user);
            log.info("修改记录数为：{}", num);
            return ResultVo.success(user,"SUCCESS",num);
        }catch (Exception e){
            return ResultVo.fail(500,"未知异常");
        }
    }

    /**
     * 批量修改用户信息
     */
    @PostMapping("/batchUpdateUser")
    public ResultVo batchUpdateUser(@RequestBody List<User> users){
        try {
            Integer num = userService.batchUpdateUser(users);
            log.info("修改记录数为：{}",num);
            return ResultVo.success(users,"SUCCESS",num);
        }catch (Exception e){
            return ResultVo.fail(500,"未知异常");
        }
    }

}

